package com.xtaller.sdboot;

import com.xtaller.sdboot.core.generator.Code;
import com.xtaller.sdboot.core.generator.Conn;
import com.xtaller.sdboot.utils.helper.FileHelper;
import com.xtaller.sdboot.utils.helper.JsonHelper;
import lombok.var;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author : Taller
 * @date : 2020/2/7 15:13
 * @Description :
 */
public class TableGenerator {
    private String
            db = "sd-boot", table = "", model = "",
            // 配置控制器所在文件夹
            controllerPackage = "com.xtaller.sdboot.controller",
            // 配置实体所在文件夹
            modelPackage = "com.xtaller.sdboot.entity",
            // 配置Mapper所在文件夹
            mapperPackage = "com.xtaller.sdboot.mapper",
            // 配置服务所在文件夹
            servicePackage = "com.xtaller.sdboot.service",
            // 配置服务实现所在文件夹
            serviceImplPackage = "com.xtaller.sdboot.service.impl",
            // 项目目录
            basePath = "/api/src/main/java/com/xtaller/sdboot",
            description = "",
            // 数据库路径
            url = "jdbc:mysql://rm-wz9q06ay2un376ua0do.mysql.rds.aliyuncs.com:3306/" + db + "?characterEncoding=utf8&serverTimezone=UTC&useSSL=false",
            // 数据库登录信息
            user = "root", password = "Wisdom!@#$%bagui";
    private StringBuffer codeModel,codeMapper,codeIService, codeServiceImpl, codeController, codeHtml, codeJs;



    public static void main(String[] args)  {
        // 如果需要生成控制器则添加一个uri,反之不需要
        var tables = JsonHelper.list(
                JsonHelper.create("name", "iam_department").put("uri", "/v1/iam/department"),
                JsonHelper.create("name", "iam_department_user").put("uri", "/v1/iam/department/user")
        );

        tables.stream().forEach(x -> {
            try {
                new TableGenerator().run(x.getString("name"), x.getString("uri"));
                System.err.println(x.getString("name") + " --> 代码生成成功");
            } catch (SQLException e) {
                e.printStackTrace();
                System.err.println(x.getString("name") + " --> 代码生成失败: 数据库连接异常");
            } catch (IOException e) {
                e.printStackTrace();
                System.err.println(x.getString("name") + " --> 代码生成失败: 其他错误");
            }

        });
    }

    public void run(String tableName, String requestMapping) throws SQLException, IOException{
        this.table = tableName;
        this.model = formatFirstTableName(table);
        File directory = new File("");
        //得到的是C:/
        var dir = directory.getCanonicalPath();
        // 生成代码文件保存路径
        String modelOutputDir = dir + basePath + "/entity" ;
        String serviceOutputDir = dir + basePath + "/service";
        String mapperOutputDir = dir + basePath + "/mapper";
        String serviceImplDir = dir + basePath + "/service/impl";
        String controllerDir = dir + basePath + "/controller";
        String htmlDir = dir + "/web/html";
        String jsDir = dir + "/web/js";

        var config = JsonHelper.create("url", url)
                .put("user", user)
                .put("password", password)
                .put("db", db)
                .put("table", tableName);

        Conn conn = new Conn(config);



        var info = conn.tableInfo();
        if(info != null) {
            while (info.next()) {
                String item = info.getString(2);
                String[] data = item.split(" ");
                description = data[0];
            }
            var column = conn.tableColumn();
            var column2 = conn.tableColumn();
            var column3 = conn.tableColumn();
            codeModel = Code.createModel(modelPackage, model, description, column);
            codeMapper = Code.createMapper(mapperPackage, modelPackage, model, description);
            codeIService = Code.createIService(servicePackage, modelPackage, model, description);
            codeServiceImpl = Code.createServiceImpl(serviceImplPackage,
                    modelPackage,
                    mapperPackage,
                    servicePackage,
                    model,
                    description);
            codeController = Code.createController(controllerPackage, modelPackage, servicePackage,
                    model, requestMapping, description);
            codeHtml = Code.createHtml(this.table);
            codeJs = Code.createJs(this.table, requestMapping, column2, column3);

        }


        var modelName = model + ".java";
        var mapperName = model + "Mapper.java";
        var serviceName = model + "Service.java";
        var serviceImplName = model + "ServiceImpl.java";
        var controllerName = model + "Controller.java";
        var htmlName = this.table + ".html";
        var jsName = this.table + ".js";

        FileHelper.write(modelOutputDir, modelName, codeModel, true);
        FileHelper.write(mapperOutputDir, mapperName, codeMapper, false);
        FileHelper.write(serviceOutputDir, serviceName, codeIService, false);
        FileHelper.write(serviceImplDir, serviceImplName, codeServiceImpl, false);
        if(requestMapping.length() > 0) {
            FileHelper.write(controllerDir, controllerName, codeController, false);
            FileHelper.write(htmlDir, htmlName, codeHtml, true);
            FileHelper.write(jsDir, jsName, codeJs, true);
        }
    }

    private static String formatFirstTableName(String field){
        String[] item = field.split("_");
        String name = "";
        for(int i=0;i<item.length;i++){
            name += item[i].substring(0, 1).toUpperCase()+ item[i].substring(1,item[i].length());
        }
        return name;
    }

}
